package level4.iso.draw
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import level4.display.GameDispose;
	import level4.display.colors.Colors;
	import level4.iso.IsoUtil;
	import level4.iso.Point3D;
	
	/**
	 * 由线绘制的iso网格
	 * */
	public class DrawIsoLines extends GameDispose
	{
		private var size:int = 25;
		
		private var rowNum:int = 10;
		private var colNum:int = 10;
		private var lineColor:uint = 0;
		private var lineAlpha:Number = 1;
		private var fillColor:uint = 0xabcdef;
		private var fillAlpha:Number = 1;
		private var matrix:Array;
		/**
		 * size : tile的尺寸
		 * rowNum : 行数
		 * colNum : 列数
		 * */
		public function DrawIsoLines()
		{
			//禁用鼠标
			mouseChildren = false;
			mouseEnabled = false;
		}
		
		override public function init(e:Event):void{
			super.init(e);
			
			drawLines();
		}
		
		override public function deInit(e:Event):void{
			super.deInit(e);
		}
		override public function dispose():void{
			graphics.clear();
			super.dispose();
		}
		
		
		public function setup(size:int,rowNum:int,colNum:int,startRow:int=0,startCol:int=0,
							  lineColor:uint=0,fillColor:uint=0x00abcdef,fillAlpha:Number=1,
							  lineAlpha:Number=1
		):void{
			this.size = size;
			this.rowNum = rowNum;
			this.colNum = colNum ;
			this.lineColor = lineColor;
			this.lineAlpha = lineAlpha;
			this.fillColor = fillColor;
			this.fillAlpha = fillAlpha;
			//1,组合数据
			matrix = [];
			//建点
			for(var i:int = startRow;i<=rowNum;i++){
				matrix[i] = [];
				
				for(var j:int = startCol;j<=colNum;j++){
					var pt3d:Point3D = new Point3D(i * size,0,j * size);
					matrix[i][j] = IsoUtil.IsoToScreen(pt3d);
				}
			}
		}
		/**
		 * 绘制 线条和填充.在 加入到现实列表时会自动调用也可以自行调用
		 * */
		public function drawLines():void{
			//基本数据验证
			if(!matrix || matrix.length==0 || !matrix[0])
				return;
			//通过
			var rowNum:int = matrix.length;
			var colNum:int = matrix[0].length;
			var i:int = 0;
			var j:int = 0;
			//2,填充背景
			graphics.clear();
			graphics.lineStyle(.1,lineColor,lineAlpha);
			graphics.beginFill(fillColor,fillAlpha);
			graphics.moveTo(matrix[0][0].x,matrix[0][0].y);
			graphics.lineTo(matrix[rowNum-1][0].x,matrix[rowNum-1][0].y);
			graphics.lineTo(matrix[rowNum-1][colNum-1].x,matrix[rowNum-1][colNum-1].y);
			graphics.lineTo(matrix[0][colNum-1].x,matrix[0][colNum-1].y);
			graphics.lineTo(matrix[0][0].x,matrix[0][0].y);
			//绘制 线条
			//行
			for(i=0;i<rowNum;i++){
				graphics.moveTo(matrix[i][0].x,matrix[i][0].y);
				for(j=0;j<colNum;j++){
					graphics.lineTo(matrix[i][j].x,matrix[i][j].y)
				}
			}
			//列
			for(j=0;j<colNum;j++){
				graphics.moveTo(matrix[0][j].x,matrix[0][j].y);
				for(i=0;i<rowNum;i++){
					graphics.lineTo(matrix[i][j].x,matrix[i][j].y)
				}
			}
			graphics.endFill();
		}
		/**
		 * 绘制 点 数组
		 * */
		public function drawPoints(pts:Array,color:uint=0xff0000,fillColor:uint=0xcccccc):void{
			if(pts && pts.length>0){
				graphics.clear();
				graphics.lineStyle(1,color);
				graphics.beginFill(fillColor);
				graphics.moveTo(pts[0].x,pts[0].y);
				
				for(var i:int=1;i<pts.length;i++){
					graphics.lineTo(pts[i].x,pts[i].y);
				}
				graphics.lineTo(pts[0].x,pts[0].y);
			}
			
		}
		
	}
}